//
//  YWAnchorHotListViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/11.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWAnchorHotListViewController.h"
#import "YWAnchorHotListModel.h"
#import "YWAnchorHotListTableViewCell.h"
#import "YWAnchorIntroduceViewController.h"

@interface YWAnchorHotListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *anchorHotListArray;

@property (nonatomic, retain) UITableView *anchorHotListTableView;

@property (nonatomic, assign) NSInteger pageId;

@property (nonatomic, assign) NSInteger maxPageId;

@end

@implementation YWAnchorHotListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.titleString;
    [self getView];
    self.anchorHotListArray = [NSMutableArray array];
    _pageId = 1;
    [self getDataByPageId:_pageId];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)getView
{
    self.anchorHotListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    _anchorHotListTableView.delegate = self;
    _anchorHotListTableView.dataSource = self;
    _anchorHotListTableView.separatorStyle = 0;
    [self.view addSubview:_anchorHotListTableView];
    
    [_anchorHotListTableView registerClass:[YWAnchorHotListTableViewCell class] forCellReuseIdentifier:@"anchorHotListCell"];
    [_anchorHotListTableView registerClass:[YWAnchorHotListTableViewCell class] forCellReuseIdentifier:@"colorNumCell"];
    
    _anchorHotListTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_anchorHotListArray removeAllObjects];
        [_anchorHotListTableView reloadData];
        _pageId = 1;
        [self getDataByPageId:_pageId];
    }];
    
    _anchorHotListTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageId++;
        if (_pageId > _maxPageId) {
            [_anchorHotListTableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [self getDataByPageId:_pageId];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWAnchorIntroduceViewController *vc = [[YWAnchorIntroduceViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.uid = [[self.anchorHotListArray objectAtIndex:indexPath.row] uid];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _anchorHotListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * FITHEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWAnchorHotListModel *anchorHotListModel = [_anchorHotListArray objectAtIndex:indexPath.row];
    if (indexPath.row < 4) {
        YWAnchorHotListTableViewCell *anchorHotListCell = [tableView dequeueReusableCellWithIdentifier:@"colorNumCell"];
        anchorHotListCell.anchorHotListModel = anchorHotListModel;
        anchorHotListCell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        if (indexPath.row == 0) {
            anchorHotListCell.numLabel.textColor = [UIColor redColor];
        }
        else if (indexPath.row == 1)
        {
            anchorHotListCell.numLabel.textColor = [UIColor orangeColor];
        }
        else if (indexPath.row == 2)
        {
            anchorHotListCell.numLabel.textColor = [UIColor greenColor];
        }
        return anchorHotListCell;
    }
    YWAnchorHotListTableViewCell *anchorHotListCell = [tableView dequeueReusableCellWithIdentifier:@"anchorHotListCell"];
    anchorHotListCell.anchorHotListModel = anchorHotListModel;
    anchorHotListCell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return anchorHotListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWAnchorHotListModel *model = [_anchorHotListArray objectAtIndex:indexPath.row];
    YWAnchorIntroduceViewController *dvc = [[YWAnchorIntroduceViewController alloc] init];
    dvc.uid = model.uid;
    [self.navigationController pushViewController:dvc animated:YES];
}

- (void)getDataByPageId:(NSInteger)pageId
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/anchor?device=android&key=%@&pageId=%ld&pageSize=20&statEvent=pageview%%2Franklist", _key, pageId];
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        _maxPageId = [[result objectForKey:@"maxPageId"] integerValue];
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWAnchorHotListModel *listModel = [[YWAnchorHotListModel alloc] initWithDic:dic];
            [_anchorHotListArray addObject:listModel];
        }
        [_anchorHotListTableView reloadData];
        [_anchorHotListTableView.mj_header endRefreshing];
        [_anchorHotListTableView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
